﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAL;
using Model;

namespace BLL
{
    /// <summary>
    /// Author: Sissi Zeng
    /// Date：2012-08-08 21:25
    /// get course List from DAL(View)
    /// </summary>
    public class CourseSearch
    {
        /// <summary>
        /// Field 
        /// </summary>
        private string _cmdStr;
        private Condition _condiction;
        /// <summary>
        /// Condiction
        /// </summary>
        public Condition Condiction
        {
            get { return _condiction; }
            set { _condiction = value; }
        }
        /// <summary>
        /// CmdStr
        /// </summary>
        public string CmdStr
        {
            get { return _cmdStr; }
            set { _cmdStr = value; }
        }
        /// <summary>
        /// Constructor
        /// no parameter
        /// </summary>
        public CourseSearch()
        {

        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="condition">Search Condition</param>
        /// <param name="cmdStr">Select Statement</param>
        public CourseSearch(Condition condition, string cmdStr)
        {
            _condiction = condition;
            _cmdStr = cmdStr;

        }


        /// <summary>
        /// Pass condition and sql to DAL(CourseList)
        /// </summary>
        /// <param name="condition">Search Condition</param>
        /// <returns>DataSet</returns>
        public DataSet generateReport(Condition condition)
        {
          
            //handler input
            if (condition.CourseName.Length == 0) {
                condition.CourseName = "";
            }else if(condition.CourseName.Length>0){
                  string temp = condition.CourseName;
                condition.CourseName = "%" + temp + "%";
            }
            if (condition.TeacherName.StartsWith("--")) {
                condition.TeacherName = "";
            }
            if (condition.LevelName.StartsWith("--")) {
                condition.LevelName = "";
            }

            //call DAL
            DAL.CourseListView dalCLV = new DAL.CourseListView();
            return dalCLV.searchCourse(condition);

        }

        /// <summary>
        /// Logic Judgement
        /// generate sql command string according to different condition
        /// </summary>
        /// <returns></returns>
        public string getCmdStr()
        {
            CmdStr = "select * from CourseList where 1=1";

            if (Condiction.CourseStatus >= 0)
            {
                CmdStr += " and CourseStatus=@CourseStatus";
            }
            if (Condiction.TeacherName.Length > 0)
            {
                CmdStr += " and TeacherName=@TeacherName";
            }
            if (Condiction.LevelName.Length > 0)
            {
                CmdStr += " and LevelName=@LevelName";
            }
            if (Condiction.CourseName.Length > 0)
            {
                CmdStr += " and CourseName like @CourseName";
                string temp = Condiction.CourseName;
                Condiction.CourseName = "%" + temp + "%";

            }
            return CmdStr;
        }
    }
}
